package org.opens.javaskill.thread.demo.myLock;

/**
 * @Date 2020/8/15 15:39
 * @Created by 99126
 */
public class SynchronizedRpoblem {

    /**
     * 功能:
     *      验证interrupt方法是否能打断抢锁导致的block状态;
     * 输出:
     * ```
     * t1 进入锁
     * t2 interrupt true
     * ```
     * 说明:
     *      1. 可以发现, t2确实被打断了, 但是还是没有抢到锁, 因为t1不会释放锁, 所以打断无法影响锁的状态.
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            run();
        }, "t1");
        t1.start();
        Thread.sleep(1000);
        Thread t2 = new Thread(() -> {
            run();
        }, "t2");
        t2.start();
        Thread.sleep(2000);
        t2.interrupt();
        System.out.println("t2 interrupt " + t2.isInterrupted());
    }

    private synchronized static void run() {
        System.out.println(Thread.currentThread().getName() + " 进入锁");
        while (true) {

        }
    }

}
